{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------------\n",
    "* 本学习记录人：许智超\n",
    "* 学习时间：2021-11-09\n",
    "* week11\n",
    "* 参考的文档：\n",
    "> 1. [python官方文档](https://www.python.org/doc/)\n",
    "> 2. [许老师博客](https://www.cnblogs.com/xuzhichao)\n",
    "\n",
    "--------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "\n",
    "## 1. 用户输入(input)\n",
    "\n",
    "> 1. 数据从 服务端定义 --> 客户端 用户输入数据 的转变\n",
    "> 2. 注意：input需要用户执行完，才可以执行后面的代码\n",
    "\n",
    "* 实践： 用户登录\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账号：xiaoming\n"
     ]
    }
   ],
   "source": [
    "username = input(\"请输入您的账号：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaoming\n"
     ]
    }
   ],
   "source": [
    "print(username)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账号：Alex\n",
      "登陆成功~\n"
     ]
    }
   ],
   "source": [
    "# 1.假如有一个用户数据（账号）在数据库  username_data  中\n",
    "\n",
    "username_data = \"Alex\"  # 服务端 定义数据\n",
    "\n",
    "username = input(\"请输入您的账号：\") # 客户端 数据（用户输入、用户交互行为等）\n",
    "\n",
    "# 2. 如果用户输入的账号等于数据库账号，登陆成功(条件判断)\n",
    "if username == username_data:\n",
    "    print(\"登陆成功~\")\n",
    "else:\n",
    "    print(\"您输入的账号有误~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xu zhichao\n",
      "xuzhichao\n"
     ]
    }
   ],
   "source": [
    "print(\"xu\",\"zhichao\")\n",
    "print(\"xu\"+\"zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账号：Jack0101\n",
      "请输入您的密码：12345678\n",
      "登陆成功~，欢迎 Jack0101 回来~\n"
     ]
    }
   ],
   "source": [
    "# 实践： 实现用户登录系统（尝试除了账号之外，还要判断密码）\n",
    "# 服务端 定义数据：1. username_data   2. password_data\n",
    "import time\n",
    "username_database = \"Jack0101\"\n",
    "password_database = \"12345678\"\n",
    "\n",
    "# 思考：用户输入只给3次机会，3次过后，5分钟后再给输入机会\n",
    "count = 3\n",
    "while True:\n",
    "    count -= 1 # 等价于 count = count + 1\n",
    "    username = input(\"请输入您的账号：\")\n",
    "    password = input(\"请输入您的密码：\")\n",
    "    # 1. 判断账号\n",
    "    if username_database == username:\n",
    "        # 2. 账号正确的前提下，判断密码的两种可能\n",
    "        if password_database == password:\n",
    "            print(\"登陆成功~，欢迎 %s 回来~\"%(username))\n",
    "            break\n",
    "        else:\n",
    "            if count>0:\n",
    "                print(\"您输入的账号不存在或者密码有误，请重新输入！您还有 %s 次输入的机会\"%(count))\n",
    "            else:\n",
    "                print(\"您已经输入3次不正确，5秒后尝试，自己的账号都记不住！笨死了~\")\n",
    "                time.sleep(5)\n",
    "                count = 3 # 输入次数重置回3次\n",
    "    else:\n",
    "        if count>0:\n",
    "            print(\"您输入的账号不存在或者密码有误，请重新输入！您还有\",count,\"次输入的机会\")\n",
    "        else:\n",
    "            print(\"您已经输入3次不正确，5秒后尝试，自己的账号都记不住！笨死了~\")\n",
    "            time.sleep(5)\n",
    "            count = 3\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. while循环\n",
    "* while True 无限循环！什么时候用？\n",
    "> 1. 当不清楚何时结束循环时使用while True（回顾，当我们清楚循环次数时用? for + range）\n",
    "> 2. while True 也可以决定**循环次数**：使用计数器count的方法\n",
    "> 3. while True 也可以根据**条件**退出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for i in range(无穷大？)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙,缠缠绵绵到天涯... 0\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 1\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 2\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 3\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 4\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 5\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 6\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 7\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 8\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 9\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 10\n"
     ]
    }
   ],
   "source": [
    "count = 0 # 记录循环的次数\n",
    "while True:\n",
    "    print(\"你是风儿我是沙,缠缠绵绵到天涯...\",count)    \n",
    "    if count == 10:\n",
    "        break  # 停止循环，跳出 循环体 \n",
    "    count = count + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.格式化文本\n",
    "\n",
    "> 1. 占位符 %s : s 指的是 str\n",
    ">> 1. %s代表 string\n",
    ">> 2. %d代表 number\n",
    ">> 3. %f代表 float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:zhichao\n",
      "age:25\n",
      "job:IT\n",
      "salary:30000\n",
      "----- INFO OF zhichao------\n",
      "age:25\n",
      "job:IT\n",
      "salary:30000\n"
     ]
    }
   ],
   "source": [
    "# 1. 字符串的拼接:用户详细信息：\n",
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")\n",
    "\n",
    "info = '''----- INFO OF ''' + name +'''------''' + '''\n",
    "name:'''+name+'''\n",
    "age:''' + age+'''\n",
    "job:''' + job +'''\n",
    "salary:'''+salary\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:zhichao\n",
      "age:26\n",
      "job:IT\n",
      "salary:40000\n",
      "----- INFO OF zhichao ------\n",
      "name:26\n",
      "age:26\n",
      "job:IT\n",
      "salary:40000\n"
     ]
    }
   ],
   "source": [
    "# 2. 占位符：\n",
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")\n",
    "\n",
    "info = '''----- INFO OF %s ------\n",
    "name:%s\n",
    "age:%s\n",
    "job:%s\n",
    "salary:%s'''%(name,name,age,job,salary)# 按照出现的次序依次填写变量\n",
    "\n",
    "# 优势：避免字符串拼接出现的语法错误\n",
    "# 劣势：如果有很多很多占位符，顺序有可能出错，容易出现顺序的差错\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:zhichao\n",
      "age:22\n",
      "job:IT\n",
      "salary:8000\n",
      "----- INFO OF zhichao ------\n",
      "name:zhichao\n",
      "age:22\n",
      "job:IT\n",
      "salary:8000\n"
     ]
    }
   ],
   "source": [
    "# 3. 字符串的格式化：str.format()\n",
    "\n",
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")\n",
    "\n",
    "info = '''----- INFO OF {姓名} ------\n",
    "name:{姓名}\n",
    "age:{年龄}\n",
    "job:{工作}\n",
    "salary:{薪水}'''.format(年龄 = age,工作 = job,姓名 = name,薪水 = salary)\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
